﻿$(function () {
    $("#title").load("导航条.html");
});
//全局变量
let overallSituation;
//查询科室
selectAjax("SelectByResponsibleDepartment", "keshi");
selectAjax("SelectByResponsibleDepartment", "kesi");
selectAjax("SelectByResponsibleDepartment", "keshis");

function selectAjax(url, attribute) {
    $.ajax({
        url: "http://localhost:8989/hospital/FollowUpGroup/" + url,
        type: "post",
        dataType: "json",
        success: function (data) {
            for (let dataKey in data) {
                let datas = data[dataKey].responsibleDepartment;
                let id = data[dataKey].id;
                if (datas != undefined && attribute == "keshi") {
                    $("#" + attribute).after("<option class='option' value=" + id + ">" + datas + "</option>");
                }
                if (datas != undefined && attribute == "keshis") {
                    $("<option class='options' value='" + id + "'>" + datas + "</option>").prependTo("#" + attribute);
                    $('#' + attribute).selectpicker('refresh');
                }
                if (datas != undefined && attribute == "kesi") {
                    $("<option class='options' value='" + id + "'>" + datas + "</option>").prependTo("#" + attribute);
                    $('#' + attribute).selectpicker('refresh');
                }
            }
        }
    });
}

angular.module('user', []).controller('userView', function ($scope) {
        /**
         * checkbox全选/全不选
         */
        //给全选的复选框添加事件
        $(document).on("click", "#all", function () {
            // this 全选的复选框
            var userids = this.checked;
            //获取name=box的复选框 遍历输出复选框
            $("input[name=box]").each(function () {
                this.checked = userids;
            });
        });

        //给name=box的复选框绑定单击事件 在angular属性里jquery点击事件不触发 解决方法用on
        $(document).on("click", "input[name=box]", function () {
            //获取选中复选框长度
            var length = $("input[name=box]:checked").length;
            //未选中的长度
            var len = $("input[name=box]").length;
            if (length == len) {
                $("#all").get(0).checked = true;
            } else {
                $("#all").get(0).checked = false;
            }
        });
        //分页数据
        let pageUtil = {
            page: 1,
            pageNum: 5,
        };
        //条件查询方法
        $scope.selectByUserLike = function () {
            //错误信息
            let state;
            //style样式
            let style = " style='color: red'";
            //获取用户id
            let id = $("#id").val();
            //姓名
            let name = $("#name").val();
            //获取性别
            let sex = $("#sex").find("option:selected").val();
            //电话
            let phone = $("#phone").val();
            //邮箱
            let email = $("#email").val();
            //密码
            let password = $("#password").val();
            //正则表达式验证
            if ((id == null || id == "") && (name == null || name == "") && (sex == "请选择" || sex == "") && (phone == null || phone == "") && (email == null || email == "") && (password == null || password == "")) {
                state = "请至少选择/填写一个条件";
                $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(name == null || name == "") && !(/^[\u4e00-\u9fa5]{0,}$/.test(name))) {
                state = "请正确填写姓名";
                $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(phone == null || phone == "") && !(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone))) {
                state = "请正确填写手机号";
                $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(email == null || email == "") && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email))) {
                state = "请正确填写邮箱";
                $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(password == null || password == "") && !(/^\w{3,20}$/.test(password))) {
                state = "请正确填写密码";
                $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else {
                state = "检索成功";
                $("#state").html("<span style='color: #00ee00' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: #0e90d2'>" + state + "</span>");
            }
            //数据
            let data = {
                "id": id,
                "name": name,
                "sex": sex,
                "phone": phone,
                "email": email,
                "password": password
            };
            if (data.sex == "请选择" || data.sex == null) data.sex = null;
            //检索方法
            $scope.TablesAndPaginationLike(data);
        };
        // 上一页/下一页
        $scope.pages = function (status) {
            //上一页
            if (status == "-1") {
                if (pageUtil.page > 1) {
                    pageUtil.page = pageUtil.page - 1;
                    $scope.selectByUserLike();
                }
            }
            //下一页
            if (status == "+1") {
                if (pageUtil.page < $scope.pageData.remaining_pages) {
                    pageUtil.page = pageUtil.page + 1;
                    $scope.selectByUserLike();
                }
            }
            //尾页
            if (status == "" || status == null) {
                pageUtil.page = $scope.pageData.remaining_pages;
                if (pageUtil.page != 0) {
                    $scope.selectByUserLike();
                }
            }
            //跳转页
            if (status == "jump") {
                let val = $("#JumpPage").val();
                if (val == null || val == "") {
                    alert("请输入跳转页");
                    return;
                }
                if (val > $scope.pageData.remaining_pages) {
                    alert("没有下一页了");
                    return;
                }
                pageUtil.page = val;
                $scope.selectByUserLike();
            }
        };

        $scope.TablesAndPaginationLike = function (data) {
            //状态值
            let status = 0;
            //更改获取的id值
            pages("shangye");
            pages("xiaye");
            pages("weiye");
            pages("tiaozhuanye");
            //更改获取的id值
            pages("shangyes");
            pages("xiayes");
            pages("weiyes");
            pages("tiaozhuanyes");
            pages("");
            $.ajax({
                url: "http://localhost:8989/hospital/User/SelectByUserLike",
                type: "post",
                data: {
                    "page": pageUtil.page,
                    "pageNum": pageUtil.pageNum,
                    "id": data.id,
                    "name": data.name,
                    "sex": data.sex,
                    "phone": data.phone,
                    "email": data.email,
                    "password": data.password
                },
                dataType: "json",
                success: function (data) {
                    for (let dataKey in data) {
                        let datas = data[dataKey];
                        $scope.$apply(function () {
                            $scope.pageData = datas;
                            if ($scope.pageData.count == 0) {
                                $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                                $(".data").empty();
                            } else {
                                $("#column").empty();
                            }
                        });
                        for (let i = 0; i <= datas.length - 1; i++) {
                            $scope.$apply(function () {
                                $scope.data = datas;
                            });
                        }
                    }
                    //加载loading
                    $(".spinner").css("display", "none");
                },
                error: function () {
                    $("#column").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: red'>加载失败</span>");
                    $(".spinner").css("display", "none");
                }
            });
        };

        //修改触发的点击事件
        function pages(state) {
            if (state == "shangye") {
                $("#" + state).css("display", "none");
            } else if (state == "xiaye") {
                $("#" + state).css("display", "none");
            } else if (state == "weiye") {
                $("#" + state).css("display", "none");
            } else if (state == "tiaozhuanye") {
                $("#" + state).css("display", "none");
            } else if (state == "shangyes") {
                $("#" + state).css("display", "");
            } else if (state == "xiayes") {
                $("#" + state).css("display", "");
            } else if (state == "weiyes") {
                $("#" + state).css("display", "");
            } else if (state == "tiaozhuanyes") {
                $("#" + state).css("display", "");
            } else if (state == "") {
                ++status;
                if (status == 1) pageUtil.page = 1;
            }
        }

        // 上一页/下一页
        $scope.page = function (status) {
            //上一页
            if (status == "-1") {
                if (pageUtil.page > 1) {
                    pageUtil.page = pageUtil.page - 1;
                    //调用表格数据与分页数据函数
                    $scope.TablesAndPagination();
                }
            }
            //下一页
            if (status == "+1") {
                if (pageUtil.page < $scope.pageData.remaining_pages) {
                    pageUtil.page = pageUtil.page + 1;
                    //调用表格数据与分页数据函数
                    $scope.TablesAndPagination();
                }
            }
            //尾页
            if (status == "" || status == null) {
                pageUtil.page = $scope.pageData.remaining_pages;
                //调用表格数据与分页数据函数
                $scope.TablesAndPagination();
            }
            //跳转页
            if (status == "jump") {
                let val = $("#JumpPage").val();
                if (val == null || val == "") {
                    alert("请输入跳转页");
                    return;
                }
                if (val > $scope.pageData.remaining_pages) {
                    alert("没有下一页了");
                    return;
                }
                pageUtil.page = val;
                //调用表格数据与分页数据函数
                $scope.TablesAndPagination();
            }
        };
        //编辑方法
        $scope.selectByUserId = function () {
            let result = "";
            let count = 0;
            $(".check").each(function () {
                if ($(this).is(':checked')) {
                    result += $(this).val();
                    count++;
                }
            });
            if (count > 1 || count == 0) {
                alert("请选择一条数据进行编辑");
                return;
            } else {
                $("#myModal2").modal("show");
                $("#div").html("<span></span>");
                $("#buttons").html("<button onclick='update()' type='button' class='btn btn-info'>修改</button><button type='button' class='btn btn-default' data-dismiss='modal'>取消</button>");
            }
            $(".selects").find("option").attr("selected", false);
            $.ajax({
                url: "http://localhost:8989/hospital/User/SelectByUserId",
                type: "post",
                dataType: "json",
                data: {"id": result},
                success: function (data) {
                    $scope.$apply(function () {
                        $scope.datas = data;
                        //全局变量
                        overallSituation = $scope.datas;
                        //数组
                        let array = new Array();
                        for (let responsibleDepartmentKey in $scope.datas.responsibleDepartment) {
                            //下拉菜单选中状态
                            array.push($scope.datas.responsibleDepartment[responsibleDepartmentKey].id);
                        }
                        $(".selectpicker").selectpicker('val', array);
                        //角色选中项
                        for (let roleKey in $scope.datas.roles) {
                            $(".selects").find("option[value='" + $scope.datas.sex + "']").prop("selected", true);
                        }
                    });
                }
            });
        };
        //删除方法
        $scope.remove = function (status) {
            let result = "";
            let count = 0;
            $(".check").each(function () {
                if ($(this).is(':checked')) {
                    result += $(this).val() + ",";
                    count++;
                }
            });
            if (status != null && count != 0) {
                if (!confirm("确定删除这" + count + "条随访组信息?")) {
                    return;
                }
                bulkDelete();

                //批量删除方法
                function bulkDelete() {
                    $.ajax({
                        url: "http://localhost:8989/hospital/User/BulkDelete",
                        type: "post",
                        dataType: "json",
                        data: {"id": result},
                        success: function (status) {
                            if (status == true) {
                                //全选多选框设置未选中状态
                                $("#all").attr("checked", false);
                                //调用表格数据与分页数据函数
                                $scope.TablesAndPagination();
                            } else {
                                alert("删除失败,请重试");
                            }
                        }
                    });
                }
            } else {
                alert("请至少选择一条信息进行删除")
            }
        };
        //分页查所有方法
        $scope.TablesAndPagination = function () {
            $.ajax({
                url: "http://localhost:8989/hospital/User/SelectAll",
                type: "post",
                data: pageUtil,
                success: function (data) {
                    for (let dataKey in data) {
                        let datas = data[dataKey];
                        $scope.$apply(function () {
                            $scope.pageData = datas;
                            if ($scope.pageData.count == 0) {
                                $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                                $(".data").empty();
                            } else {
                                $("#column").empty();
                            }
                        });
                        for (let i = 0; i <= datas.length - 1; i++) {
                            $scope.$apply(function () {
                                $scope.data = datas;
                            });
                        }
                    }
                    //加载loading
                    $(".spinner").css("display", "none");
                },
                error: function () {
                    $("#column").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: red'>加载失败</span>");
                    $(".spinner").css("display", "none");
                }
            });
            //更改获取的id值
            page("shangyes");
            page("xiayes");
            page("weiyes");
            page("tiaozhuanyes");

            page("shangye");
            page("xiaye");
            page("weiye");
            page("tiaozhuanye");

            //修改触发的点击事件
            function page(state) {
                if (state == "shangyes") {
                    $("#" + state).css("display", "none");
                } else if (state == "xiayes") {
                    $("#" + state).css("display", "none");
                } else if (state == "weiyes") {
                    $("#" + state).css("display", "none");
                } else if (state == "tiaozhuanyes") {
                    $("#" + state).css("display", "none");
                } else if (state == "shangye") {
                    $("#" + state).css("display", "");
                } else if (state == "xiaye") {
                    $("#" + state).css("display", "");
                } else if (state == "weiye") {
                    $("#" + state).css("display", "");
                } else if (state == "tiaozhuanye") {
                    $("#" + state).css("display", "");
                }
            }
        };
        $scope.TablesAndPagination();
        //添加方法
        $scope.insert = function () {
            //错误信息
            let state;
            //style样式
            let style = " style='color: red'";
            let styles = "style='color: #00cc03' class='glyphicon glyphicon-warning-sign'";
            //获取账户名
            let username = $("#usernames").val();
            //获取姓名
            let name = $("#names").val();
            //获取电话
            let phone = $("#phones").val();
            //获取邮箱
            let email = $("#emails").val();
            //获取密码
            let password = $("#passwords").val();
            //获取性别
            let sex = $(".sex:selected").val();
            //获取科室多选值
            let val = $(".selectpickers").selectpicker('val');

            let data = {
                username: username,
                name: name,
                phone: phone,
                email: email,
                password: password,
                sex: sex
            };
            //正则表达式
            if ((data.username == "" || data.username == null) && (data.name == "" || data.name == null) && (data.phone == "" || data.phone == null) && (data.email == "" || data.email == null) && (data.password == "" || data.password == null) && (data.sex == undefined || data.sex == "" || data.sex == null)) {
                state = "请填写添加的信息";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (data.username == "" || data.username == null) {
                state = "请填写账户名";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(/^\w+.{3,20}$/.test(data.username))) {
                state = "请正确填写账户名";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (data.name == "" || data.name == null) {
                state = "请填写姓名";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(name))) {
                state = "请正确填写姓名";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (data.phone == null || data.phone == "") {
                state = "请填写手机号";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone))) {
                state = "请正确填写手机号";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (data.email == null || data.email == "") {
                state = "请填写邮箱";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email))) {
                state = "请正确填写邮箱";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (data.password == null || data.password == "") {
                state = "请填写密码";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (!(/^.{6,20}$/.test(password))) {
                state = "请正确填写密码(6-20)位";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (sex == undefined || sex == null || sex == "") {
                state = "请选择性别";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else if (val == null) {
                state = "请选择科室";
                $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
                return;
            } else {
                state = "";
                $("#divs").html("<span></span>");
            }

            $.ajax({
                url: "http://localhost:8989/hospital/User/Insert",
                type: "post",
                data: data,
                dataType: "json",
                success: function (status) {
                    if (status == true) {
                        selectByUserId(data.name);
                    } else {
                        state = "操作失败";
                        $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
                    }
                },
                error: function () {
                    state = "操作失败";
                    $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
                }
            });

            function selectByUserId(name) {
                $.ajax({
                    url: "http://localhost:8989/hospital/User/SelectByUserName",
                    type: "post",
                    dataType: "json",
                    data: {
                        "name": name
                    },
                    success: function (data) {
                        for (let dataKey in data) {
                            for (let datumKey in data[dataKey]) {
                                insert(data[dataKey][datumKey].id);
                            }
                        }
                    }
                });
            }

            function insert(userId) {
                $.ajax({
                    url: "http://localhost:8989/hospital/UserDepartment/InsertDepartment",
                    type: "post",
                    dataType: "json",
                    data: {
                        "userId": userId,
                        "departmentId": val
                    },
                    success: function (status) {
                        if (status == true) {
                            setTimeout(function () {
                                $("#myModal1").modal('hide');
                                state = "操作成功";
                                $("#column").html("<div " + styles + "></div>&nbsp;" + "<span style='color: #2aabd2'>" + state + "</span>");
                                $('body').on('hidden.bs.modal', '#myModal1', function () {
                                    $('#myModal1 input').val('');
                                    $('#myModal1 select').prop("value", '请选择');
                                    $("#kesi").selectpicker('deselectAll');
                                    setTimeout(function () {
                                        $("#divs").html('<span></span>');
                                        $("#column").html("<div></div>");
                                    }, 2000);
                                });
                            }, 500);
                            $scope.TablesAndPagination();
                        } else {
                            state = "操作失败";
                            $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
                        }
                    },
                    error: function () {
                        state = "操作失败";
                        $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
                    }
                });
            }
        }
    }
);

//修改方法
function update() {
    //style样式
    let style = "style='color: #f8c815' class='glyphicon glyphicon-warning-sign'";
    let styles = "style='color: red'";
    //错误信息
    let status;
    //获取科室多选值
    let val = $(".selectpicker").selectpicker('val');
    //获取性别
    let sex = $(".sexs:selected").val();
    //删除对象中的科室
    delete overallSituation.responsibleDepartment;
    delete overallSituation.sex;
    overallSituation["sex"] = sex;
    //正则表达式验证
    if (overallSituation.name == null || overallSituation.name == "") {
        status = "请填写姓名";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!/^[\u4e00-\u9fa5]{0,}$/.test(overallSituation.name)) {
        status = "请正确填写姓名";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituation.phone == null || overallSituation.phone == "") {
        status = "请填写电话";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(overallSituation.phone))) {
        status = "请正确填写电话";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituation.email == null || overallSituation.email == "") {
        status = "请填写邮箱";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(overallSituation.email)) {
        status = "请正确填写邮箱";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (sex == undefined || sex == null || sex == "") {
        status = "请选择性别";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (val == null) {
        status = "请选择科室";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else {
        status = null;
    }
    if (status == null) {
        $.ajax({
            url: "http://localhost:8989/hospital/User/Update",
            type: "post",
            dataType: "json",
            data: overallSituation,
            success: function (state) {
                if (state == true) {
                    $.ajax({
                        url: "http://localhost:8989/hospital/UserDepartment/UpdateDepartment",
                        type: "post",
                        dataType: "json",
                        data: {
                            "userId": overallSituation.id,
                            "departmentId": val
                        },
                        success: function (state) {
                            if (state == true) {
                                status = "修改成功";
                                $("#column").html("<span style='color: #00cc03'>" + status + "</span>");
                                $("#all").attr("checked", false);
                                $("#myModal2").modal("hide");
                                //获取angular的controller 调用方法
                                var $scope = angular.element('#userView').scope();
                                //调用查询方法
                                $scope.TablesAndPagination();
                                setTimeout(function () {
                                    $("#column").empty();
                                }, 5000)
                            } else {
                                status = "修改失败";
                                $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
                            }
                        }
                    });
                }
            }
        });
    }
}